package com.fuelstations.coderone;





import com.fuelstations.coderone.DatabaseMetadata.StationTableMetadata;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseOpenHelper extends SQLiteOpenHelper {
	public static final String TAG = "DatabaseOpenHelper";

	public DatabaseOpenHelper(Context context, String databaseName, int version) {
		super(context, databaseName, null, version);
	}

	public static final String DB_CREATE_STATION = "CREATE TABLE "
			+ StationTableMetadata.TABLE_NAME + " ("
			+ StationTableMetadata.STATION_ID + " INTEGER PRIMARY KEY, "
			+ StationTableMetadata.COMPANY + " TEXT NOT NULL, "
			+ StationTableMetadata.OWNER + " TEXT NOT NULL, "
			+ StationTableMetadata.CITY + " TEXT NOT NULL, "
			+ StationTableMetadata.ADDRESS + " TEXT NOT NULL, "
			+ StationTableMetadata.DISTRICT + " TEXT NOT NULL, "
			+ StationTableMetadata.POSTALCODE + " INTEGER NOT NULL, "
			+ StationTableMetadata.UNLD95 + " REAL, "
			+ StationTableMetadata.UNLD98 + " REAL, "
			+ StationTableMetadata.UNLD100 + " REAL, "
			+ StationTableMetadata.DIESEL + " REAL, "
			+ StationTableMetadata.DIESEL_HEATING + " REAL, "
			+ StationTableMetadata.KEROSENE + " REAL, "
			+ StationTableMetadata.OWNER_EN + " TEXT, "
			+ StationTableMetadata.CITY_EN + " TEXT, "
			+ StationTableMetadata.ADDRESS_EN + " TEXT, "
			+ StationTableMetadata.DISTRICT_EN + " TEXT, "
			+ StationTableMetadata.LAT + " REAL, "
			+ StationTableMetadata.LNG + " REAL); ";

	
 

	@Override
	public void onCreate(SQLiteDatabase db) {

		try {
			db.execSQL(DB_CREATE_STATION);


		} catch (SQLException sqle) {
			Log.e(TAG, sqle.getMessage());
		}

	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + StationTableMetadata.TABLE_NAME);

		this.onCreate(db);
	}
}
